
@extends('layout.layer')

@section('layout.content') 
<div class="row">
  <div class="col-xs-12">
    <form class="form-horizontal" id="staff-update" autocomplete="off">
    <input type="hidden" name="_method" value="put" />
    <div class="form-group">
        <label class="col-xs-12 control-label" for="username"> <b><span class="red">*</span> 账户：</b> </label>
        <div class="col-xs-12">
            <input type="text" class="form-control" id="username" name="username" placeholder="账户" value="{{$staff->username}}" />
        </div>        
    </div>
    <div class="form-group">
        <label class="col-xs-12 control-label no-padding-right" for="name">  名称：</label>
        <div class="col-xs-12">
            <input type="text" class="form-control" id="name" name="name" placeholder="名称"  value="{{$staff->name}}" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-12 control-label" for="branch">  <b><span class="red">*</span>所属分支：</b></label>
        <div class="col-xs-12">
          <div class="radio">
            @foreach($branches as $branch)
            <label class="inline no-padding-left radio-label">
              <input name="branch" type="checkbox" value="{{$branch->id}}" class="ace ace-checkbox-2 input-lg" {{$staff->branch_id == $branch->id?'checked':'' }} >
              <span class="lbl"> {{$branch->name}}</span>
            </label>
            @endforeach
          </div>
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-12 control-label" for="role">  角色：</label>
        <div class="col-xs-12">
          <div class="radio">
          @foreach($roles as $role)
            <label class="inline no-padding-left radio-label">
              <input name="role" type="checkbox" value="{{$role->id}}" class="ace ace-checkbox-2 input-lg" 
              @foreach($staff->roles as $staff_role)
              {{ $staff_role->id == $role->id ? 'checked':'' }}
              @endforeach
              />
              <span class="lbl"> {{$role->name}}</span>
            </label>       
          @endforeach
        </div>
      </div>
    </div>

    <hr />
    <div class="form-group">
      <div class="col-xs-12 center">
          <button type="button" class="btn btn-primary submit">
            <i class="ace-icon fa fa-check"></i>
            <span class="">确定</span>
          </button>
          <button type="button" class="btn btn-grey cancel" style="margin-left: 10px;">
            <i class="ace-icon fa fa-close"></i>
            <span class="">取消</span>
          </button>
      </div>
    </div>
    
  </form>
  </div>
</div>

@stop

@section('javascript')
<script type="text/javascript">
  $(function() {

    $('.submit').on('click', function() {

      var roles = new Array(); 
      $("input[name='role']:checked").each(function(){  
          roles.push($(this).val());
      });
      
      var uri = '{{route('system.staff.update', $staff->id)}}';
      
      $.post(uri, {
        '_token':$("input[name='_token']").attr('value'),
        '_method': $("input[name='_method']").attr('value'),
        'username': $("input[name='username']").val(),
        'name': $("input[name='name']").val(),
        'branch': $("input[name='branch']:checked").val(),
        'roles': roles,
        'is_submit': true
      }, function(data) {
        layer.msg(data.message, {time: 2000,shift: -1}, function () {
          if (data.status === true) {
            parent.location.reload();
            parent.layer.close(parent.layer.index);
          }
        });
      },'json').error(function(data){
        layer.msg(data.responseJSON.message);
      }); 
      
    });

    $("input:checkbox[name='branch']").on('click', function (e) {
        $("input:checkbox[name='branch']").not(this).attr("checked", false);
    });
   

    $('.cancel').on('click', function() {
      parent.layer.closeAll();
    });

  });
</script>
@stop